﻿CREATE PROCEDURE [ServiceEngine].[CreateEndpoint]
	@Name nvarchar(400),
	@BindingID uniqueidentifier,
	@PolicyID uniqueidentifier,
	@Description ntext = null,
	@NamespaceURI ntext = null,
	@TestCallsAccepted bit = 0,
	@AutoOperationPubUpdate bit = 0,
	@AutoOperationActivationUpdate bit = 0,
	@Configuration xml = null,
	@ServiceName nvarchar(400) = null,
	@Port smallint = null,
	@ID uniqueidentifier OUTPUT
	
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	SET NOCOUNT ON;

	SET @ID = newid();

	BEGIN TRY
		BEGIN TRANSACTION

		INSERT INTO ServiceEngine.Endpoints (EndpointID, BindingID, PolicyID, NamespaceURI, TestCallsAccepted, AutoOperationPubUpdate, AutoOperationActivationUpdate, Configuration, ServiceName, Port)
		VALUES (@ID, @BindingID, @PolicyID, @NamespaceURI, @TestCallsAccepted, @AutoOperationPubUpdate, @AutoOperationActivationUpdate, @Configuration, @ServiceName, @Port)

		BEGIN TRY
			INSERT INTO ServiceEngine.Names (EntityID, EntityTypeID, EntityName, EntityDescription)
			VALUES (@ID, 2, @Name, @Description)
		END TRY
		BEGIN CATCH

			SELECT 
				@ErrorMessage = 'Attempt to insert Endpoint with duplicate URI failed',
				@ErrorSeverity = 11,
				@ErrorState = ERROR_STATE();

			RAISERROR (@ErrorMessage, 
					   @ErrorSeverity,
					   1);
		END CATCH

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH
END


/****** Object:  StoredProcedure [ServiceEngine].[GetEndpoint]    Script Date: 05/19/2008 03:14:56 ******/
SET ANSI_NULLS ON


